﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class oderCheck : System.Web.UI.Page
{
    BLL.Business.UserBLL userBLL = new BLL.Business.UserBLL();
    BLL.Business.BookBLL bookBLL = new BLL.Business.BookBLL();
    BLL.Business.OrderBLL orderBLL = new BLL.Business.OrderBLL();
    Model.Order order = new Model.Order();

    DataTable dt_product = new DataTable();  // 新建数据库表
    protected void Page_Load(object sender, EventArgs e)
    {
        lblUserName.Text = Session["UserName"].ToString();
        BindGridList();
        BindUserTable();
    }
    // 绑定产品列表
    public void BindGridList()
    {
        try
        {
            dt_product.Columns.Add("Book_Idint");
            dt_product.Columns.Add("Book_Namestr");  // 书名
            dt_product.Columns.Add("Book_Price");    // 价格
            dt_product.Columns.Add("Book_Count");    // 数量

            float SumPrice = 0;      // 总价
            if (!object.Equals(Session["orderList"], null))
            {
                Hashtable hash = (Hashtable)Session["orderList"];
                DataRow dr;
                foreach (DictionaryEntry de in hash)
                {
                    dr = dt_product.NewRow();
                    int bookId = Convert.ToInt32(de.Key.ToString());  // 商品编号
                    int count = Convert.ToInt32(de.Value.ToString()); // 商品数量
                    DataSet ds = bookBLL.SelectByBookId(bookId);      // 获得产品信息

                    float price = 0;   // 原价
                    float Discount = 0;// 折扣 
                    price = float.Parse(ds.Tables[0].Rows[0]["Book_Priceflo"].ToString());
                    Discount = float.Parse(ds.Tables[0].Rows[0]["Book_Discountflo"].ToString());

                    dr["Book_Idint"] = ds.Tables[0].Rows[0]["Book_Idint"].ToString();
                    dr["Book_Namestr"] = ds.Tables[0].Rows[0]["Book_Namestr"].ToString();
                    dr["Book_Price"] = price * Discount;           // 价格=原价*折扣
                    dr["Book_Count"] = count;

                    dt_product.Rows.Add(dr);
                    SumPrice += price * Discount * count;
                }
            }
            this.orderList.DataSource = dt_product.DefaultView;
            this.orderList.DataBind();
            this.sumPrice.Text = SumPrice.ToString();
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.Message.ToString() + "')</script>");
        }
    }
    // 绑定收货人信息
    public void BindUserTable()
    {
        try
        {
            DataSet ds = userBLL.SelectByName(Session["UserName"].ToString());

            this.lblUserId.Text = ds.Tables[0].Rows[0]["User_Idint"].ToString();      // 用户编号
            this.txtAddress.Text = ds.Tables[0].Rows[0]["User_Addressstr"].ToString();
            this.txtEmail.Text = ds.Tables[0].Rows[0]["User_Mailstr"].ToString();
            this.txtLinkMan.Text = ds.Tables[0].Rows[0]["User_Namestr"].ToString();
            this.txtPhone.Text = ds.Tables[0].Rows[0]["User_Telephonestr"].ToString();
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.Message.ToString() + "')</script>");
        }
    }
    /// <summary>
    /// 提交订单
    /// 首先添加订单
    /// 其次为每个产品 添加一个订单详情
    /// 同时修改产品库存数量
    /// </summary>
    protected void btnSure_Click(object sender, EventArgs e)
    {
        try
        {
            String userId = this.lblUserId.Text.ToString();
            String sumPrice = this.sumPrice.Text.ToString();
            String address = this.txtAddress.Text.Trim().ToString();
            String email = this.txtEmail.Text.Trim().ToString();
            String linkman = this.txtLinkMan.Text.Trim().ToString();
            String phone = this.txtPhone.Text.Trim().ToString();
            String postalcode = this.txtPost.Text.Trim().ToString();
            String remark = this.txtRemark.Text.Trim().ToString();
            String delivary = this.dropSMode.SelectedItem.Text.Trim().ToString();
            String payment = this.dropFMode.SelectedItem.Text.Trim().ToString();

            order.User_Idint = Int32.Parse(userId);
            order.Ord_ContactManstr = linkman;
            order.Ord_Emailstr = email;
            order.Ord_Telephonestr = phone;
            order.Ord_PostalCodestr = postalcode;
            order.Ord_Addressstr = address;
            order.Ord_Statusint = 1;             // 对于用户来说交易是否成功（下订单及成功） 
            order.Ord_IsCheckedint = 0;          // 订单是否审批（发货，结账）
            order.Ord_Deliverystr = delivary;
            order.Ord_Paymentstr = payment;
            order.Ord_Remarkstr = remark;
            order.Ord_Datedate = DateTime.Now;

            if (orderBLL.InsertOrderTransaction(order, dt_product))
            {
                Response.Write("<script>alert('订单成功！')</script>");
            }
            else
            { 
                Response.Write("<script>alert('订单失败，请检查信息！')</script>");
            }
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('"+ex.Message.ToString()+"')</script>");
        }      
    }
}
